package com.example.administrator.college02.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.college02.BaseFragment;
import com.example.administrator.college02.R;
import com.example.administrator.college02.adapter.CommonAdapter;
import com.example.administrator.college02.adapter.RecyclerViewAdapter;
import com.example.administrator.college02.adapter.ViewHolder;
import com.example.administrator.college02.model.ItemMessage;
import com.example.administrator.college02.utils.CommonNet;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/7/12.
 */
public class FragmentSocietyChild extends BaseFragment {

    private String TAG = "FragmentSocietyChild";
    @BindView(R.id.lv_society)
    ListView lvSociety;
    private CommonAdapter<ItemMessage> mAdapter;
    private List<ItemMessage> mList;
    RecyclerViewAdapter adapter ;
    //item 显示所需(仅供DEMO)
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.view_pager_society,container,false);
        unbinder = ButterKnife.bind(this,view);
        mContext = getContext();
        getDataFromServer();
        mAdapter = new CommonAdapter<ItemMessage>(mContext,R.layout.item_type_01) {
            @Override
            public void conver(ViewHolder holder, ItemMessage itemMessage) {

                holder.setText(R.id.tv_item1_text,itemMessage.getTitle());
                holder.setPhotoFromNet(mContext,R.id.btn_icon01, CommonNet.BASE_URL+itemMessage.getIcon01());
                holder.setPhotoFromNet(mContext,R.id.btn_icon02, CommonNet.BASE_URL+itemMessage.getIcon02());
                holder.setPhotoFromNet(mContext,R.id.btn_icon03, CommonNet.BASE_URL+itemMessage.getIcon03());
            }
        };
        lvSociety.setAdapter(mAdapter);
        return view;
    }
    private void getDataFromServer() {
        StringRequest request = new StringRequest(Request.Method.GET, CommonNet.SOCIETY_NEWS,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        mList = new ArrayList<>();
                        try {
                            JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject object = jsonArray.getJSONObject(i);
                                Log.d(TAG,object.toString());
                                String title = object.getString("school_news_title");
                                String urlImage = object.getString("school_news_thumb");
                                String type = object.getString("school_news_type");
                                com.alibaba.fastjson.JSONArray ja = JSON.parseArray(urlImage);
                                Log.d(TAG,"---------ja"+ja.toString());
                                if (type.equals("3")){
                                    ItemMessage message = new ItemMessage(title,ja.getString(0),ja.getString(1),ja.getString(2));
                                    mList.add(message);
                                }
                                if (type.equals("2")){
                                    ItemMessage message = new ItemMessage(title,"2",ja.getString(0));
                                    mList.add(message);
                                }
                                if (type.equals("1")){
                                    ItemMessage message = new ItemMessage(title,"1",ja.getString(0));
                                    mList.add(message);
                                }
                                if (type.isEmpty()){
                                    ItemMessage message = new ItemMessage(title);
                                    mList.add(message);
                                }
                            }
                            mAdapter.bindData(mList);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        Volley.newRequestQueue(mContext).add(request);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
